(function(){
	angular.module("cartCtrlmodule",[])
		.controller("cartController",["$scope","bookCartDataService",
            "postDataService","userDataService","HOST","$http",
            "USERDATA","$ionicPopup","$timeout",
            "$http","HOST","$ionicLoading","$location",
            "$rootScope","proNumFactory",
			function($scope,bookCartDataService,
                    postDataService,userDataService,HOST,$http
                    ,USERDATA,$ionicPopup,$timeout,
                    $http,HOST,$ionicLoading,$location,$rootScope,proNumFactory
                     ){
                $scope.cartList = bookCartDataService.cartList;
                $scope.submitOrder = function(){
                    console.log(USERDATA.user);
                    // // 
                    // $http.post(HOST+"order.php",
                    //     {
                    //         user_id:USERDATA.userData.user_id,
                    //         cart:bookCartDataService.cartList
                    //     }
                    // ).success(function(data){
                    //     console.log(data);
                    // }).error(function(error){
                    //     console.log(error);
                    // });
                    if(USERDATA.user==undefined){
                        var alertPopup = $ionicPopup.alert({
                           title: '提 示',
                           template: '您尚未登陆！'
                        });
                        $timeout(function() {
                            alertPopup.close();
                            // 发广播 要求首页登录
                            $rootScope.$broadcast("orderLogin");
                        },2000);
                    }else{
                        if(USERDATA.isLogin==undefined){
                            // console.log("注销了");
                            var alertPopup = $ionicPopup.alert({
                               title: '提 示',
                               template: '您尚未登陆222！'
                            });
                            $timeout(function() {
                                alertPopup.close();
                                // 发广播 要求首页登录
                                $rootScope.$broadcast("orderLogin");
                            },2000);
                        }else{
                            console.log(USERDATA.user.user_id);
                            $location.path("order/"+USERDATA.user.user_id);
                            

                        }   
                    }
                
                }
				
                $scope.cartNum = 0;
                $scope.total = 0;
                // bookCartDataService.cartList[id].num++;
                $scope.cartJian = function(gid){
		            $scope.cartList[gid]['num']--;
		            if($scope.cartList[gid]['num']<=0){
		                delete $scope.cartList[gid];
		            }
         	 	}

            	$scope.cartJia = function(gid){
                	$scope.cartList[gid]['num']++;
            	}
                $scope.$watch("cartList",function(){
                    var total = 0;
                    var num = 0;
                    for(var i in $scope.cartList){
           
                        num++;
                        total += $scope.cartList[i]['price']*$scope.cartList[i]['num'];
                    }


                    $scope.total = total;
                    proNumFactory.total = total;
                },true);
                $scope.$on("loginSuccess",function(){
                    console.log("loginSuccess");
                    $scope.isLogin = true;
                });
                
		}])
})();

